package com.piao.springboot_resttemplate.util;

import com.piao.springboot_resttemplate.entity.MyMessage;
import com.piao.springboot_resttemplate.entity.Word;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HttpUtil {
    static RestTemplate restTemplate = new RestTemplate();


    //RestTemplate getForObject  GET请求 一个/多个参数(URI)
    public static Word getWordById(Integer id) {
        Word word = restTemplate.getForObject(
                "http://localhost:8080/Word/{id}", Word.class, id);

        ResponseEntity<Word> responseEntity = restTemplate.getForEntity(
                "http://localhost:8080/Word/{id}", Word.class, id);
        responseEntity.getStatusCode();//返回HttpStatus
        responseEntity.getStatusCodeValue();//返回状态值int
        Word word2 = responseEntity.getBody();//返回请求返回的JSON对应的对象
        HttpHeaders httpHeaders = responseEntity.getHeaders();//获取请求头信息 HttpHeaders对象
        httpHeaders.getContentType();//获取请求内容类型

        //使用Map封装多个参数
        Map<String, Object> params = new HashMap<>();
        params.put("id", 89);
        params.put("english", "like");
        params.put("chinese", "喜欢");
        String url = "http://localhost:8080/Word/{id}/{english}/{chinese}";
        Word word3 = restTemplate.getForObject(url, Word.class, params);


        //打印该word
        System.out.println(word);
        return word;
    }

    //RestTemplate getForEntity GET请求 多个参数(URI)
    public static List<Word> getWordList() {
        //使用Map封装多个参数
        Map<String, Object> params = new HashMap<>();
        params.put("id", 3);
        params.put("english", "like");

        //返回的集合封装在ResponseEntity内
        ResponseEntity<List> responseEntity = restTemplate.getForEntity(
                "http://localhost:8080/Word/{id}", List.class, params);
        List<Word> wordList = responseEntity.getBody();

        //打印该word
        System.out.println(wordList);
        return wordList;


    }


    //JOSN参数


    //POST请求
    //        restTemplate.postForObject();
//        restTemplate.postForEntity();
    public void post() {

        HttpHeaders httpHeaders = new HttpHeaders();
        //设置请求头 参数类型为JSON
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        Word word = new Word(8989, "testWord", "测试单词释义");//作为参数传JSON
        HttpEntity<Word> request = new HttpEntity<>(word, httpHeaders);

        //URL 请求信息 返回结果映射
        MyMessage myMessage = restTemplate.postForObject("http://localhost:8080/Word", request, MyMessage.class);
    }


    public void put() {
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置请求头 参数类型为JSON
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        Word word = new Word(8989, "testWord", "测试单词释义");//作为参数传JSON
        HttpEntity<Word> request = new HttpEntity<>(word, httpHeaders);

        //URI单参数  同理若为多个参数：使用Map<String, Object>封装即可
        Integer id=10;
        restTemplate.put("http://localhost:8080/Word/{id}",request,id);
    }

    public void delete() {


        //URL URI参数
        restTemplate.delete("", 1);

        //URL 多参数
        //使用Map封装多个参数
        Map<String, Object> params = new HashMap<>();
//        params.put("id", 3);
        restTemplate.delete("", params);


        //JSON参数
        HttpHeaders httpHeaders = new HttpHeaders();
        //设置请求头 参数类型为JSON
        httpHeaders.setContentType(MediaType.APPLICATION_JSON);
        Word word = new Word(8989, "testWord", "测试单词释义");//作为参数传JSON
        HttpEntity<Word> request = new HttpEntity<>(word, httpHeaders);

        restTemplate.delete("", request, Word.class);
    }
}
